#!/usr/bin/python

import json, urllib

"""
This script retrieves all online EU probes outside of a specified AS and its immediate neighbours
"""

#Get all probes, with their IDs, country codes, status code and ASN numbers
#------------------------------------------------------------------------------------------------------------
response = urllib.urlopen("https://atlas.ripe.net/api/v1/probe/?fields=status,asn,id,country_code&limit=77")
jsondata = json.loads(response.read())
(true,false,null) = (True,False,None)
result = eval(str(jsondata))
#------------------------------------------------------------------------------------------------------------


#Filter out all non-connected probes
probe = [i for i in result.get("objects") if int(i.get("status")) == 1]


#Filter out non-EU probes
#------------------------------------------------------------------------------------------------------------
euCountryCodes = ['AD', 'AL', 'AT', 'BE', 'BG', 'BY', 'CZ', 'DE', 'DK', 'EE', 'FI', 'FR', 'GR', 'HU', 'IE', 'IS', 'IT', 'LI', 'LT', 'LU', 'LV', 'MK', 'MT', 'NL', 'NO', 'PL', 'PT', 'RO', 'RU', 'SE', 'SI', 'SK', 'SM', 'UA', 'VA', 'BA', 'HR', 'MD', 'MC', 'ME', 'RS', 'ES', 'CH', 'GB']
probe = [i for i in probe if i.get("country_code") in euCountryCodes]
#------------------------------------------------------------------------------------------------------------




#Filter out all probes of target ASN and its immediate neighbours
#------------------------------------------------------------------------------------------------------------
#Get target ASN neighbours
excludedASN = [32934]
response = urllib.urlopen("https://stat.ripe.net/data/asn-neighbours/data.json?resource=32934")
jsondata = json.loads(response.read())
(true,false,null) = (True,False,None)
result = eval(str(jsondata))
excludedASN = excludedASN + [i.get("asn") for i in result.get("data").get("neighbours")]
probe = [{i.get("country_code"):i.get("id")} for i in probe if i.get("asn") not in excludedASN]
#------------------------------------------------------------------------------------------------------------

countryProbeList = dict()

for i in probe:
        if i.keys()[0] not in countryProbeList.keys():
                countryProbeList[i.keys()[0]] = [i.values()[0]]
        else:
                countryProbeList[i.keys()[0]].append(i.values()[0])
    
                
probeCount = 0
dictCount = 0
co

finalSelection = list()
while probeCount <= 499:
        if probeCount >= 499:
                break
        else:
                if dictCount >= len(countryProbeList.keys()):
                        dictCount = 0
                
                
                
                probeCount = probeCount +1 
                dictCount = dictCount +1 

#Build a list of equally distributed probes across EU countries

        
